package com.cgbl.cg.ui.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.CourseProfileCommentAdapter
import com.cgbl.cg.databinding.AppFragmentResourceCommentBinding
import com.cgbl.cg.vm.mine.ResourceProfileViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.model.CommentBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener

class ResourceCommentFragment :
    GLBaseFragment<AppFragmentResourceCommentBinding, ResourceProfileViewModel>() {

    private lateinit var courseProfileCommentAdapter: CourseProfileCommentAdapter

    override fun initContentView(
        inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_resource_comment

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        courseProfileCommentAdapter =
            CourseProfileCommentAdapter(AppConstants.BundleValue.COMMENT_TYPE_RESOURCE)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = courseProfileCommentAdapter
        }
        courseProfileCommentAdapter.addChildClickViewIds(R.id.ll_zan)
        courseProfileCommentAdapter.addChildClickViewIds(R.id.item_layout)
        courseProfileCommentAdapter.onLikeListener =
            object : CourseProfileCommentAdapter.OnLikeListener {
                override fun like(itemPosition: Int, position: Int) {
                    viewModel.likeComment(
                        courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.id
                            ?: ""
                    ) {
                        courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.like =
                            if (courseProfileCommentAdapter.data[itemPosition].commentList?.get(
                                    position
                                )?.like == "1"
                            ) {
                                "0"
                            } else {
                                "1"
                            }

                        courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.likeCount =
                            if (courseProfileCommentAdapter.data[itemPosition].commentList?.get(
                                    position
                                )?.like == "1"
                            ) {
                                "${
                                    (courseProfileCommentAdapter.data[itemPosition].commentList?.get(
                                        position
                                    )?.likeCount ?: "0").toInt() + 1
                                }"
                            } else {
                                "${
                                    (courseProfileCommentAdapter.data[itemPosition].commentList?.get(
                                        position
                                    )?.likeCount ?: "0").toInt() - 1
                                }"
                            }

                        courseProfileCommentAdapter.notifyItemChanged(itemPosition)
                    }
                }
            }
        courseProfileCommentAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.ll_zan -> {
                    viewModel.likeComment(courseProfileCommentAdapter.data[position].id ?: "") {
                        courseProfileCommentAdapter.data[position].like =
                            if (courseProfileCommentAdapter.data[position].like == "1") {
                                "0"
                            } else {
                                "1"
                            }

                        courseProfileCommentAdapter.data[position].likeCount =
                            if (courseProfileCommentAdapter.data[position].like == "1") {
                                "${(courseProfileCommentAdapter.data[position].likeCount ?: "0").toInt() + 1}"
                            } else {
                                "${(courseProfileCommentAdapter.data[position].likeCount ?: "0").toInt() - 1}"
                            }

                        courseProfileCommentAdapter.notifyItemChanged(position)
                    }
                }
                else -> {
                    val comment = courseProfileCommentAdapter.getItem(position);
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.Mine.A_MINE_COMMENT_INPUT,
                        Bundle().apply {
                            putInt(
                                AppConstants.BundleKey.COMMENT_PRODUCT_TYPE,
                                AppConstants.BundleValue.COMMENT_TYPE_RESOURCE
                            )
                            putString(AppConstants.BundleKey.COMMENT_PRODUCT_ID, comment.courseId)
                            putString(AppConstants.BundleKey.COMMENT_PARENT_ID, comment.id)
                            putString(
                                AppConstants.BundleKey.COMMENT_ACCOUNT_ID,
                                comment.fromAccountId
                            )
                        })
                }
            }

        }
        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.refresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
            }
        })
    }

    fun setCourseProfileData(courseId: String) {
//        viewModel.fetchCourseInfo(courseId)
        viewModel.fetchCourseComment(courseId)
    }

    override fun initViewObservable() {
        viewModel.uc.refreshCommentEvent.observe(this) {
            if (it.isEmpty()) {
                val emptyView = layoutInflater.inflate(R.layout.app_layout_empty, null)
                courseProfileCommentAdapter.setEmptyView(emptyView)
            }
            courseProfileCommentAdapter.setNewInstance(it as MutableList<CommentBean>?)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.loadMoreCommentEvent.observe(this) {
            courseProfileCommentAdapter.addData(it)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        LiveDataEventManager.observerCommentSuccessEvent(this) {
            viewModel.refresh()
        }
    }
}